/*
 * To change this template, choose Tools \t Templates
 * and open the template in the editor.
 */
package network;

import SQL.SQL;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import weibo4j.model.WeiboException;

/**
 *
 * @author Administrator
 */
public class setStatusModule {

    public static int importantFriends = 30;

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, FileNotFoundException, IOException, WeiboException, InterruptedException {
        Auth.Auth4Code.prepareWeibo();
        DataPreparation data = new DataPreparation();
        relationScore.relationScore(data);
        setStatusModule(data);
    }

    public static void setStatusModule(DataPreparation data) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
       // setInterestStatusModule(data);
        System.out.println("interest ready");
        setImportantStatusModule(data);
        System.out.println("important ready");
        //setZoombieAgencyStatusModule(data);
        //System.out.println("zoombie ready");
    }

    public static void setImportantStatusModule(DataPreparation data) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        List<String> sortInfluence = new ArrayList<String>();
        Set keys = data.influence.keySet();
        int count = keys.size();
        for (int i = 0; i < count; i++) {
            String key = (String) keys.toArray()[i];
            double value = data.influence.get(key)*1000000;
            String vk = value + "\t" + key;
            sortInfluence.add(vk);
            
        }
        Collections.sort(sortInfluence);
        int in=0;
       
        int size = importantFriends;
        if (count < size) {
            size = count;
        }
        System.out.println(count);
        for (int i = 0; i < size; i++) {
            String fid = sortInfluence.get(count-i-1).split("\t")[1];
            System.out.println(sortInfluence.get(count-i-1));
            System.out.println(data.data.username.get(fid));
            String query = "SELECT `no` FROM `wb_demo_status` WHERE `uid`= '" + Config.uid + "' and `fid`='" + fid + "';";
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            //建立到MySQL的连接
            Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/weibonews?useUnicode=true&characterEncoding=gb2312", "root", "");
            //执行SQL语句
            Statement stmt = (Statement) conn.createStatement();
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()) {
                String no=rs.getString("no");
                String[] values={"重要微博"};
                String[] columns={"module"};
                String table="wb_demo_status";
                SQL.SQL(SQL.updateValue(columns, values, table, "no", no));
                
            }
        }
    }

    public static void setZoombieAgencyStatusModule(DataPreparation data) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        String query="SELECT `fid`,`no` FROM `wb_demo_status` ";
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        //建立到MySQL的连接
        Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/weibonews?useUnicode=true&characterEncoding=gb2312", "root", "");
        //执行SQL语句
        Statement stmt = (Statement) conn.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while(rs.next()){
            String fid=rs.getString("fid");
            String no=rs.getString("no");
            if(data.data.zoombieIDs.contains(fid)){
                String[] values={"营销类微博"};
                String[] columns={"module"};
                String table="wb_demo_status";
                SQL.SQL(SQL.updateValue(columns, values, table, "no", no));
            }else if(data.data.agencyIDs.contains(fid)){
                String[] values={"机构媒体微博"};
                String[] columns={"module"};
                String table="wb_demo_status";
                SQL.SQL(SQL.updateValue(columns, values, table, "no", no));
            }
        }
    }
   
    public static void setInterestStatusModule(DataPreparation data) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        getInterest.getInterest(data);
        ResultSet status = getUidStatus();
        while (status.next()) {
            String fid = status.getString("fid");
            String no=status.getString("no");
            if (getInterest.friendModule.containsKey(fid)) {
                String[] values={getInterest.friendModule.get(fid)};
                String[] columns={"module"};
                String table="wb_demo_status";
                SQL.SQL(SQL.updateValue(columns, values, table, "no", no));
            }
        }
    }

    public static ResultSet getUidStatus() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String query = "SELECT `no`,`fid` FROM `wb_demo_status` WHERE `uid`= '" + Config.uid + "';";
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        //建立到MySQL的连接
        Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/weibonews?useUnicode=true&characterEncoding=gb2312", "root", "");
        //执行SQL语句
        Statement stmt = (Statement) conn.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        return rs;
    }
}
